// subcontract/Mine/addAddress/index.js
import {
  AddressParse
} from './addressParseBundle.js';
import { areaList } from './area-data';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    areaList,
    fieldValue: '',
    cascaderValue: '',
    checked: false,//默认地址
    textarea: '',//地址识别
    name: '',//姓名
    phone: '',//电话
    province: '', //省
    city: '',//市
    district: '',//区
    address: '',//详细地址
    areaTree: [],
    fieldNames: {
      text: 'title',
      value: 'id',
      children: 'children',
    },
  },
  /**双向数据绑定 */
  chengInput(event) {
    let type = event.target.dataset.type
    console.log(event);
    let content = event.detail.value
    const typeMap = {
      textarea: 'textarea',
      name: 'name',
      phone: 'phone',
      address: 'address'
    };
    this.setData(typeMap[type] ? { [typeMap[type]]: content } : '');
  },
  /**添加地址 */
  async address() {
    if (!getApp().validatePhoneNumber(this.data.phone)) return getApp().showToast('请输入正确手机号码！')
    let object = {
      id: this.data.addressId,
      contact: this.data.name,
      telephone: this.data.phone,
      province_id: this.data.province,
      city_id: this.data.city,
      district_id: this.data.district,
      address: this.data.address,
      is_default: this.data.checked ? 1 : 0,
    }
    if (this.data.addressId != '') {
      console.log(this.data.addressId);
      const { code, data } = await getApp().api.useraddressUpdate(object)
      if (code == 1) {
        wx.navigateBack()
      }
    } else {
      const { code, data } = await getApp().api.address(object)
      if (code == 1) {
        wx.navigateBack()
      }
    }


  },
  /** 识别 地址  */
  autoParse() {
    if (!this.data.textarea) return getApp().showToast('请粘贴您需要识别的地址')
    // const address = this.address
    const result = AddressParse.parse(this.data.textarea)
    console.log(result[0]);
    if (result && result.length) {
      let html = ''
      for (let key in result[0]) {
        if (result[0][key]) {
          html += key + `:` + result[0][key] + `<br/>`
        }
      }
      console.log(result);
      this.setData({
        name: result[0].name,
        phone: result[0].mobile,
        district: result[0].area,
        province: result[0].province,
        city: result[0].city,
        address: result[0].details,
      })
      console.log(this.data.city);
    }
  },
  /**清空信息 */
  clear() {
    this.setData({
      name: '',
      phone: '',
      city: '',
      address: '',
      textarea: ''
    })
  },
  onClick() {
    this.setData({
      show: true,
    });
  },
  /**默认地址 */
  onChange(e) {
    this.setData({
      checked: !this.data.checked
    })
  },
  onClose() {
    this.setData({
      show: false,
    });
  },

  confirm(e) {
    console.log(e);
    let values = e.detail.values
    this.setData({
      district: values[2].name,
      province: values[0].name,
      city: values[1].name,
    })
    this.onClose()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      addressId: options.id || ''
    })
    let TTUL = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('useraddressUpdate', function (item) {
      console.log(item)
      TTUL.setData({
        name: item.contact,
        phone: item.telephone,
        district: item.district,
        province: item.province,
        city: item.city,
        address: item.address,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})